package com.aijia.custom.product.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.aijia.custom.product.R;
import com.aijia.custom.product.data.ProcessData;
import com.aijia.custom.product.utils.DateUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * Created by yejianjun on 2019/1/16.
 */

public class NoScrollGridAdapter extends BaseAdapter {
    private List<ProcessData> mList = new ArrayList<>();
    private Context mContext;
    private HashMap<Integer, View> viewMap;

    public NoScrollGridAdapter(Context context, List<ProcessData> dataList) {
        mContext = context;
        viewMap = new HashMap<>();
        mList = dataList;
    }

    public void updateData(int artId, int status, String finishDate) {
        for (int i = 0; i < mList.size(); i++) {
            if (mList.get(i).getId() == artId) {
                mList.get(i).setStatus(status);
                mList.get(i).setFinishDate(finishDate);
                notifyDataSetChanged();
                break;
            }
        }
    }

    public void setData(ArrayList<ProcessData> data) {
        if (null != data) {
            mList = data;
        }
    }

    @Override
    public int getCount() {
        return mList.size();
    }

    @Override
    public Object getItem(int position) {
        return mList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final Holder holder;
        if (!viewMap.containsKey(position) || viewMap.get(position) == null) {
            holder = new Holder();
            convertView = LayoutInflater.from(mContext).inflate(R.layout.item_gridview, null);
            holder.nameTV = convertView.findViewById(R.id.process_item_name);
            holder.statusIV = convertView.findViewById(R.id.process_item_status);
            holder.dateTV = convertView.findViewById(R.id.process_item_date);
            convertView.setTag(holder);
            viewMap.put(position, convertView);
        } else {
            convertView = viewMap.get(position);
            holder = (Holder) convertView.getTag();
        }

        holder.nameTV.setText(mList.get(position).getName());
        holder.dateTV.setText(mList.get(position).getFinishDate() == null ? "" : DateUtils.getFormatDate(mList.get(position).getFinishDate()));
        if (mList.get(position).getStatus() == 3) {
            holder.statusIV.setBackgroundResource(R.drawable.icon_green);
            holder.dateTV.setVisibility(View.VISIBLE);
        } else if (mList.get(position).getStatus() == 2) {
            holder.statusIV.setBackgroundResource(R.drawable.icon_yellow);
            holder.dateTV.setVisibility(View.VISIBLE);
        } else if (mList.get(position).getStatus() == 4) {
            holder.statusIV.setBackgroundResource(R.drawable.half_color);
            holder.dateTV.setVisibility(View.VISIBLE);
        } else {
            holder.statusIV.setBackgroundResource(R.drawable.icon_red);
            holder.dateTV.setVisibility(View.INVISIBLE);
        }

        return convertView;
    }

    private class Holder {
        ImageView statusIV;
        TextView nameTV;
        TextView dateTV;
    }


}
